//
//  main.m
//  Province-City-Zone
//
//  Created by dllo on 16/7/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "City.h"
#import "Zone.h"
#import "Province.h"
#import "TempCity.h"
int main(int argc, const char * argv[]) {
#if 0
  // 第四题
    
    NSString *path = @"/Users/dllo/DLS10710/Objective-C/HomeWork/Province-City-Zone/Province-City-Zone/area.txt";
  // 拿到数据源字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    
     //根据回车， 分割字符串
    NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
   //1.省数组 － 省字典 － 市数组 － 市字典 － 区数组 － 区字典
    NSMutableArray *allProvinceArray = [NSMutableArray array];
    for (NSString *area in allArray) {
   // 取到所有的省（没有空格开头）
        if (![area hasPrefix:@" "]) {
   // 创建了省字典（字典中包含两个字段 name array）
            NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
   //  添加proName 字段
            [proDic setObject:area forKey:@"proName"];
  //  创建了省中所有市的数组
            NSMutableArray *cityArray = [NSMutableArray array];
   // 添加数组字段
            [proDic setObject:cityArray forKey:@"cityArray"];
   // 将创建好的省字典， 放到省数组中
            [allProvinceArray addObject:proDic];
   // 取到所有的市（两个空格开头，但不是四个空格开头）
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
    //难点： 通过lastObject获取到对应的省字典
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
   //  拿到省字典中对应得市数组
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
   //  创建 市字典（name 和 区Array）
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            [cityDic setObject:area forKey:@"cityName"];
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDic setObject:zoneArray forKey:@"zoneArray"];
    //  向市数组中添加市字典
            [cityArray addObject:cityDic];
// 取到所有的区／县
        } else {
    // 通过省数组获取当前所在省字典
            NSMutableDictionary *proDic = [allProvinceArray lastObject];
    // 通过省字典获取到市数组
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
    // 通过市数组获取到对应的市字典
            NSMutableDictionary *city = [cityArray lastObject];
    // 通过市字典获取到对应得区／县的数组
            NSMutableArray *zoneArray = [city objectForKey:@"zoneArray"];
    // 最终将 区／县 添加到数组中            
            [zoneArray addObject:area];
            
        }
    }
//    NSLog(@"%@", allProvinceArray);
  // 2.省数组 － Province类
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [Province zoneWithName:area];
            [allProArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *pro = [allProArray lastObject];
            City *city = [City zoneWithName:area];
            [pro.array addObject:city];
        } else {
           City *city = [[[allProArray lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
        }
    }
    NSLog(@"%@", allProArray);
#endif
#if 1
//   第五题
    NSString *locationPath = @"/Users/dllo/DLS10710/Objective-C/HomeWork/Province-City-Zone/Province-City-Zone/Location1.txt";
    NSString *source = [NSString stringWithContentsOfFile:locationPath encoding:NSUTF8StringEncoding error:nil];
    // 根据， 分割字符串， 得到所有城市数组
    NSArray *allCityArray = [source componentsSeparatedByString:@","];
    // 根据每个城市中的｜处理字符串， 重新生成字典的数组
    NSMutableArray *allCitDicArray = [NSMutableArray array];
    for (NSString *city in allCityArray) {
   // 根据｜切割字符串
        NSArray *arrayCity = [city componentsSeparatedByString:@"|"];
   // 根据切割生成的数组， 生成新的城市字典
        NSMutableDictionary *cityDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:[arrayCity firstObject],@"cityName", [arrayCity lastObject], @"cityNumber", nil];
   //  添加到新数组
        [allCitDicArray addObject:cityDic];
    }
   //  排序（重要）
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allCitDicArray sortUsingDescriptors:@[sort]];
//    NSLog(@"%@", allCitDicArray);
    NSMutableArray *finalArray = [NSMutableArray array];
    for (NSMutableDictionary *cityDic in allCitDicArray) {
        NSString *cityName = cityDic[@"cityName"];
        NSString *cityNumber = cityDic[@"cityNumber"];
        if ([cityNumber hasSuffix:@"0000"]) {
            NSMutableDictionary *proDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            
            [proDic setObject:[NSMutableArray array] forKey:@"cityArray"];
            [finalArray addObject:proDic];
            
        } else if ([cityNumber hasSuffix:@"00"] && ![cityNumber hasSuffix:@"0000"]) {
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [dicCity setObject:[NSMutableArray array] forKey:@"zoneArray"];
            [cityArray addObject:dicCity];
        } else {
//        NSMutableArray *zoneArray = [[[[finalArray lastObject] objectForKey:@"cityArray"] lastObject] objectForKey:@"zoneArray"];
            NSMutableDictionary *proDic = [finalArray lastObject];
            NSMutableArray *cityArray = [proDic objectForKey:@"cityArray"];
            NSMutableDictionary *dicCity = [cityArray lastObject];
            NSMutableArray *zoneArray = [dicCity objectForKey:@"zoneArray"];
            
            NSMutableDictionary *zoneDic = [NSMutableDictionary dictionaryWithDictionary:cityDic];
            [zoneArray addObject:zoneDic];
        }
    }
    
    
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
   // 根据｜切割字符串
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithName:[city firstObject] number:[city lastObject]];
        [allTempCityArray addObject:tempCity];
    }
    //对 类数组 排序
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityNumber" ascending:YES];
    [allCitDicArray sortUsingDescriptors:@[sortTempCity]];
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (TempCity *tempCity in allTempCityArray) {
        if ([tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [Province zoneWithTempCity:tempCity];
            [finalClassArray addObject:pro];
        } else if ([tempCity.cityNumber hasSuffix:@"00"] && ![tempCity.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [finalClassArray lastObject];
            City *city = [City zoneWithTempCity:tempCity];
            [pro.array addObject:city];
        } else {
            Province *pro = [finalClassArray lastObject];
            City *city = [pro.array lastObject];
            Zone *zone = [Zone zoneWithTempCity:tempCity];
            [city.array addObject:zone];
        }
    }
    NSLog(@"%@", finalArray);
#endif
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    return 0;
}
